package cn.tedu.fresheveryday.controller;

import cn.tedu.fresheveryday.pojo.entity.Order;
import cn.tedu.fresheveryday.pojo.entity.ShopCart;
import cn.tedu.fresheveryday.pojo.entity.User;
import cn.tedu.fresheveryday.pojo.dto.OrderInsertDTO;
import cn.tedu.fresheveryday.pojo.dto.OrderPagerDTO;
import cn.tedu.fresheveryday.pojo.vo.OrderListVO;
import cn.tedu.fresheveryday.restful.JsonResult;
import cn.tedu.fresheveryday.service.IOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@Api(tags = "订单模块")
@RequestMapping(path = "/order", produces = "application/json;charset=utf-8")
public class OrderController {
    @Autowired
    private IOrderService service;

    /**
     * 添加一个购物车的订单
     * @param orderInsertDTO 添加订单dto
     * @param session HttpSession对象
     * @return
     */
    @PostMapping("/insert")
    @ApiOperation("根据购物车添加订单")
    public JsonResult<Void> insert(@RequestBody OrderInsertDTO orderInsertDTO, HttpSession session) {
        log.info("根据购物车添加订单...");
        User user = (User) session.getAttribute("user");

        if (user != null) {
            Order order = new Order();

            order.setUserId(user.getId()); // 设置用户id
            BeanUtils.copyProperties(orderInsertDTO, order);

            ShopCart shopCart = (ShopCart) session.getAttribute("shopCart");

            if (shopCart != null) {
                // 生成订单
                service.insert(order, shopCart);

                return JsonResult.success();
            }

            return JsonResult.error("购物车不存在...");
        }

        return JsonResult.error("您当前登录状态已失效，请登录后重试。");
    }

    /**
     * 生成订单
     * @param orderInsertDTO 添加订单dto
     * @param session HttpSession对象
     * @return
     */
    @ApiOperation("生成订单")
    @PostMapping("/generate")
    public JsonResult<Void> generate(@RequestBody OrderInsertDTO orderInsertDTO, HttpSession session) {
        log.info("开始生成订单...");
        // 从session获取用户信息
        User user = (User) session.getAttribute("user");

        // 用户已登录
        if (user != null) {
            Order order = new Order();

            order.setUserId(user.getId()); // 设置用户id
            BeanUtils.copyProperties(orderInsertDTO, order);

            service.generate(order); // 生成订单

            return JsonResult.success();
        }

        return JsonResult.error("您当前未登录，请登录后重试。");
    }

    /**
     * 通过id删除订单订单
     * @param id 订单id
     * @return
     */
    @GetMapping("/delete")
    @ApiOperation("通过id删除订单订单")
    public JsonResult<Void> deleteById(Integer id) {
        service.deleteById(id);

        return JsonResult.success("删除成功", null);
    }

    /**
     * 分页查询用户的订单
     * @param session
     * @return
     */
    @GetMapping("/select")
    @ApiOperation("分页查询用户的订单")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "page", name="page", example = "1", required = true),
            @ApiImplicitParam(value = "size", name="size", example = "5", required = true)
    })
    public Map<String, Object> selectByUserId(int page, int size, HttpSession session) {
        User user = (User) session.getAttribute("user");

        if (user != null) {
            OrderPagerDTO orderPagerDTO = new OrderPagerDTO();
            String userId = user.getId();

            orderPagerDTO.setSize(size); // 设置每页记录数
            orderPagerDTO.setUserId(userId); // 设置用户id
            orderPagerDTO.setOffset((page - 1) * size); // 设置偏移量

            List<OrderListVO> rows = service.selectByUserId(orderPagerDTO);
            int total = service.selectCountByUserId(userId);

            return JsonResult.returnResultMap(total, rows);
        }

        return null;
    }

    /**
     * 通过订单状态分页查询用户订单
     * @param orderPagerDTO
     * @param session
     * @return
     */
    @GetMapping("/selectByState")
    @ApiOperation("通过订单状态分页查询用户的订单")
    public Map<String, Object> selectByStateAndUserId(OrderPagerDTO orderPagerDTO, HttpSession session) {
        User user = (User) session.getAttribute("user");

        if (user != null) {
            String userId = user.getId();

            int page = orderPagerDTO.getPage();
            int size = orderPagerDTO.getSize();

            orderPagerDTO.setUserId(userId); // 设置用户id
            orderPagerDTO.setOffset((page - 1) * size); // 设置偏移量

            List<OrderListVO> rows = service.selectByStateAndUserId(orderPagerDTO);
            int total = service.selectCountByStateAndUserId(orderPagerDTO.getState(), userId);

            return JsonResult.returnResultMap(total, rows);
        }

        return null;
    }

    /**
     * 再来一单
     * @param id
     * @return
     */
    @GetMapping("/again")
    @ApiOperation("再来一单功能")
    @ApiImplicitParam(value = "id", name="id", required = true)
    public JsonResult<Void> again(Integer id) {
        service.again(id);

        return JsonResult.success("成功将商品添加到购物车", null);
    }

    /**
     * 订单支付
     * @param id
     * @return
     */
    @GetMapping("/pay")
    @ApiOperation("订单支付功能")
    @ApiImplicitParam(value = "id", name="id", required = true)
    public JsonResult<Void> pay(Integer id) {
        service.pay(id);

        return JsonResult.success("订单支付成功", null);
    }

    /**
     * 订单接单/配送
     * @param id
     * @return
     */
    @ApiOperation("订单接单功能")
    @GetMapping("/distribute")
    @ApiImplicitParam(value = "id", name="id", required = true)
    public JsonResult<Void> distribute(Integer id) {
        service.distribute(id);

        return JsonResult.success("接单成功", null);
    }

    /**
     * 修改订单状态为“已送达”
     * @param id
     * @return
     */
    @ApiOperation("修改订单状态为“已送达”")
    @GetMapping("/arrive")
    @ApiImplicitParam(value = "id", name="id", required = true)
    public JsonResult<Void> arrive(Integer id) {
        service.arrive(id);

        return JsonResult.success("订单已送达", null);
    }

    /**
     * 确认收货
     * @param id
     * @return
     */
    @GetMapping("/receipt")
    @ApiOperation("确认收货功能")
    @ApiImplicitParam(value = "id", name="id", required = true)
    public JsonResult<Void> receipt(Integer id) {
        service.receipt(id);

        return JsonResult.success("确认收货完成", null);
    }

    /**
     * 取消订单
     * @param id
     * @return
     */
    @GetMapping("/cancel")
    @ApiOperation("取消订单功能")
    @ApiImplicitParam(value = "id", name="id", required = true)
    public JsonResult<Void> cancel(Integer id) {
        service.cancel(id);

        return JsonResult.success("订单取消成功", null);
    }

}